#include <iostream>
#include "Protocol.hpp"


class Calculator
{
    Respond CalHelper(Request& rq)
    {
        int result = 0 ,code = 0;        
        switch(rq.Get_Op())
        {
            case '+':
            result = rq.Get_Data1() + rq.Get_Data2();
            break;

            case '-':
            result = rq.Get_Data1() - rq.Get_Data2();
            break;

            case '*':
            result = rq.Get_Data1() * rq.Get_Data2();
            break;

            case '/':
            {   
            if(rq.Get_Data2() == 0) code = 1;    
            else result = rq.Get_Data1() / rq.Get_Data2();
            }
            break;
            
            case '%':
            {   
            if(rq.Get_Data2() == 0) code = 2;    
            else result = rq.Get_Data1() % rq.Get_Data2();
            }
            break;

            default:
            code = 3;
        }
        Respond rp(result , code);
        return rp;
    }
public:
    std::string operator()(std::string& package)
    {
        // 对报文进行解包 + 反序列化 + 计算 + 构造response + 序列化 + 加报文
        std::string content = Decode(package);
        if(content == "") return "";

        std::cout << content <<std::endl;

        Request rq;
        rq.deserialize(content);
        Respond rp = CalHelper(rq);

        std::string ret_content = rp.serialize();
        std::string ret_package = Encode(ret_content);

        return ret_package;
    }
};
